/*
 * Copyright (c) 2008, Yifu Huang
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of SleekIce nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.sleekice.gui;

import com.sleekice.core.Renderable;
import com.sleekice.core.Updatable;
import com.sleekice.input.Clickable;
import com.sleekice.object.Velocity;
import com.sleekice.util.GL;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Point2D;

public class Button implements Updatable, Renderable,
    Clickable, Widget {

    /* button colors */
    private static final Color DEF_BUTTON_COLOR = GL.bluePowder;
    private static final Color DEF_BUTTON_HOVER_COLOR = GL.blueSky;
    private static final Color DEF_BUTTON_PRESS_COLOR =
        GL.blueDodger;
    private static final Color DEF_BORDER_COLOR = Color.black;
    private Color buttonColor_ = DEF_BUTTON_COLOR;
    private Color buttonHoverColor_ = DEF_BUTTON_HOVER_COLOR;
    private Color buttonPressColor_ = DEF_BUTTON_PRESS_COLOR;
    private Color borderColor_ = DEF_BORDER_COLOR;
    /* button bounds */
    protected Rectangle bounds;
    
    public void setButtonColor(Color buttonColor) {
        buttonColor_ = buttonColor;
    }
    public void setButtonHoverColor(Color buttonHoverColor) {
        buttonHoverColor_ = buttonHoverColor;
    }
    public void setButtonPressColor(Color buttonPressColor) {
        buttonPressColor_ = buttonPressColor;
    }
    public void setBorderColor(Color borderColor) {
        borderColor_ = borderColor;
    }
    
    /* for mouse input */
    private boolean mouseOver;
    private boolean mousePressed;
    private boolean clicked;

    private boolean bobbleOnMouseOver;
    private Velocity velocity;
    protected Point2D offsetPoint;
    private int elastic;

    public Button(int x, int y) {
        this(x,y,100,20);
    }
    public Button(int x, int y, int width, int height) {
        mouseOver = false;
        mousePressed = false;
        clicked = false;

        velocity = new Velocity();
        offsetPoint = new Point2D.Double(0, 0);
        
        bobbleOnMouseOver = true;
        
        bounds = new Rectangle(x, y, width, height);
    }

    public boolean isMousePressed() {
        return mousePressed;
    }

    public boolean isMouseOver() {
        return mouseOver;
    }

    public void render(Graphics2D g) {
        if (mousePressed) {
            g.setColor(buttonPressColor_);
        }
        else {
            if (mouseOver) {
                g.setColor(buttonHoverColor_);
            }
            else {
                g.setColor(buttonColor_);
            }
        }
        g.fillRect(bounds.x + (int) offsetPoint.getX(),
            bounds.y + (int) offsetPoint.getY(), bounds.width, bounds.height);
        g.setColor(borderColor_);
        g.drawRect(bounds.x + (int) offsetPoint.getX(),
            bounds.y + (int) offsetPoint.getY(), bounds.width, bounds.height);
    }
    
    public void relocate(int x, int y) {
        bounds.setLocation(x, y);
    }

    public void resize(int width, int height) {
        bounds.setSize(width, height);
    }

    public void mouseOver() {
        mouseOver = true;
        if (bobbleOnMouseOver) {
            velocity.setDirection(270);
            velocity.setSpeed(80);
            elastic = 10;
        }
    }

    public void mouseLeave() {
        mouseOver = false;
    }

    public void mousePress() {
        mousePressed = true;
    }

    public void mouseRelease() {
        mousePressed = false;
    }

    public Rectangle getMouseArea() {
        return bounds;
    }

    public void mouseClick() {
        clicked = true;
        mousePressed = false;
    }

    public boolean isClicked() {
        if (clicked) {
            clicked = false;
            return true;
        }
        return false;
    }
    
    public void setBobbleOnMouseOver(boolean bobble) {
        bobbleOnMouseOver = bobble;
    }

    public void update(double elapsedTime) {
        if (velocity.getSpeed() > 0) {
            velocity.updatePos(offsetPoint, elapsedTime);
            if (elastic < 0) {
                if (offsetPoint.getY() > 0 && velocity.getDirection() == 270) {
                    velocity.setSpeed(0);
                    offsetPoint.setLocation(0, 0);
                }
                else if (offsetPoint.getY() < 0 && velocity.getDirection() == 90) {
                    velocity.setSpeed(0);
                    offsetPoint.setLocation(0, 0);
                }
            }
            else {
                if (offsetPoint.getY() > elastic &&
                    (velocity.getDirection() == 270 || velocity.getDirection() == -90)) {
                    velocity.setDirection(90);
                    elastic -= 6;
                }
                else if (offsetPoint.getY() < -elastic &&
                    velocity.getDirection() == 90) {
                    velocity.setDirection(270);
                    elastic -= 6;
                }
            }
        }
    }

    private boolean visible_ = true;
    public boolean isVisible() {
        return visible_;
    }

    public void setVisible(boolean visible) {
        visible_ = visible;
    }

    public boolean isEnabled() {
        return true;
    }

    public void setEnabled(boolean enabled) {
    }
}